#region Using Directives

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Eneta.Portal.Forum.Controls.Base;
using Eneta.Portal.Forum.Controls.Common;
using Eneta.Portal.Forum.Core.Domain.Entities;
using Eneta.Portal.Forum.Core.Service.Application;

#endregion

namespace Eneta.Portal.Forum.Controls
{
    public class DeleteTopic : BaseForumControl
    {
        #region Fields

        private Topic topic;
        private Label lblWarning;
        private SPButton btnSubmit;
        private SPButton btnCancel;

        #endregion

        public DeleteTopic()
        {
        }

        protected override void CreateChildControls()
        {
            InitControlVariables();
            CreateControls();
            BuildUI();
        }

        private void InitControlVariables()
        {
            if(this.Page.IsPostBack)
            {
                _topicID = (int) ViewState["_topicID"];
            }
            else
            {
                ViewState["_topicID"] = _topicID;				
            }

            this.topic = RepositoryRegistry.TopicRepository.GetById(_topicID);
            base.ParentLink = ForumApplication.Instance.GetLink(SharePointForumControls.ViewTopics, "forum={0}", _forumID);
        }

        private void CreateControls()
        {
            lblWarning = new Label();
            lblWarning.Text = string.Format("Warning, you are about to delete the topic named<br>\"{0}\"<br>containing {1} posts.<p>This operation cannot be undone.<p>Are you sure?", topic.Name, topic.NumPosts);
            lblWarning.CssClass = "ms-alerttext";

            btnSubmit = new SPButton("Submit");
            btnSubmit.Click += new EventHandler(btnSubmit_Click);

            btnCancel = new SPButton("Cancel");
            btnCancel.Click += new EventHandler(btnCancel_Click);
        }

        private void BuildUI()
        {
            Controls.Add(BuildBasePageLinks());
            Controls.Add(new LiteralControl("<br>"));
	
            AddBoxHeader(String.Format("Delete Topic \"{0}\"", topic.Name), false, 2);
            Controls.Add(CreateRow(lblWarning));
            Controls.Add(CreateButtonRow(btnSubmit, btnCancel));
	
            CloseBox();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            RedirectToParent();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            RepositoryRegistry.TopicRepository.Delete(this.topic);
            RedirectToParent();
        }
    }
}